import { projectConfig, rootDir, dataDir, saveProjectConfig } from '../config.js'
import fse from 'fs-extra'
import path from 'path'
import { glob } from 'glob'
import { convertWebpageOrPdfToPdf } from '../util/pdf.js'
import md5 from 'md5'
import fs from 'fs'

class Sanfoundry {
  constructor() {}

  initRoute(koa) {
    koa.router.get('/', ctx => {
      this.start()

      ctx.body = {
        code: 0,
        msg: '任务启动',
      }
    })
  }

  async start() {
    console.log('start')

    let techniqueFiles = await glob('D:/Git/llz/project/at/attack.mitre.org/json/technique/**/*.json')
    let subTechniqueFiles = await glob('D:/Git/llz/project/at/attack.mitre.org/json/sub-technique/**/*.json')
    const files = [...techniqueFiles, ...subTechniqueFiles]

    console.log('files', files.length)

    for (let i = projectConfig.fileIndex; i < files.length; i++) {
      await this.handleContnet(files[i])
    }
  }

  async handleContnet(file) {
    const content = await fse.readJson(file, 'utf-8')
    const links = []

    if (content.references) {
      content.references.forEach(item => {
        links.push(item.url)
      })
    }

    if (content.procedureExamples) {
      content.procedureExamples.forEach(procedureExample => {
        if (procedureExample.reference.url) links.push(procedureExample.reference.url)
      })
    }

    for (let i = projectConfig.downloadIndex; i < links.length; i++) {
      await this.download(links[i])
    }

    projectConfig.fileIndex = projectConfig.fileIndex + 1
    projectConfig.downloadIndex = 0
    saveProjectConfig()
  }

  async download(url) {
    const filePath = path.join(dataDir, md5(url) + '.pdf')

    // 判断文件是否存在
    if (fs.existsSync(filePath)) {
      console.log('文件已存在，跳过')
      return
    } else {
      await convertWebpageOrPdfToPdf(url, filePath).catch(err => {
        projectConfig.errors.push({
          url,
          filePath,
          error: err.message,
        })
      })
    }

    projectConfig.downloadIndex = projectConfig.downloadIndex + 1
    saveProjectConfig()
  }
}

export default new Sanfoundry()
